package com.zhangmeng.online.exam.controller;

import com.zhangmeng.online.exam.dao.ProfessionDao;
import com.zhangmeng.online.exam.dto.Result;
import com.zhangmeng.online.exam.entity.Profession;
import com.zhangmeng.online.exam.entity.User;
import com.zhangmeng.online.exam.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zm
 * @date 2025/4/1 11:23
 * @version: 1.0
 */
@RestController
@RequestMapping("/profession")
public class ProfessionController {

    @Autowired
    private ProfessionDao professionDao;

    @RequestMapping("/list")
    public Result userList(Integer pageNum, Integer pageSize) {

        Pageable pageable = PageRequest.of(PageUtils.getPageNum(pageNum), pageSize, Sort.Direction.DESC, "addTime");
        Page<Profession> all = professionDao.findAll(pageable);
        List<Profession> list = all.getContent();
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Profession profession : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", profession.getId());
            map.put("name", profession.getName());
            resultList.add(map);
        }
        return Result.success(pageNum, pageSize, all.getTotalElements(), resultList);
    }

}
